<?php 

/*
	$Id: video.php 2012-08-23 lrb $
*/

if(!defined('IN_UCHOME')) {
	exit('Access Denied');
}


if(submitcheck('videosubmit')) {
	
	if(empty($_SGLOBAL['supe_uid'])) {
		rtjson('请先登录!');
	}
	$uid=$_SGLOBAL['supe_uid'];
	$videoid=$_POST['videoid'];
	$gd=$_POST['gd'];
	$msg=$_POST['msg'];
	$ktime1=$_POST['ktime1'];
	$ktime2=$_POST['ktime2'];
	$dtime1=$_POST['dtime1'];
	$dtime2=$_POST['dtime2'];
	
	if(empty($videoid)){
		rtjson('没有找到参数videoid!');
	}
	
	if(empty($gd)){
		rtjson('没有找到参数gd!');
	}

	if(empty($msg)){
		rtjson('没有找到参数msg!');
	}
	
	$query=$_SGLOBAL['db']->query("select * from ".tname('video')." where videoid=".$videoid);

	$video=$_SGLOBAL['db']->fetch_array($query);
	
	
	$query=$_SGLOBAL['db']->query("select cid,dateline from ".tname('video_comment')." where uid='$uid' order by dateline desc");
	
	if($row=$_SGLOBAL['db']->fetch_array($query)){
		if(time()-$row['dateline']<60){
			rtjson('请休息一下在发表分享!');
		}
	}
	
	$msg=trim($_POST['msg']);
	
	$ktime=$ktime1*60+$ktime2*1;
	$dtime=$dtime1*60+$dtime2*1;
	
	$arr=array(
		'videoid'=>$videoid,
		'uid'=>$_SGLOBAL['supe_uid'],
		'username'=>$_SGLOBAL['supe_username'],
		'ty'=>$_POST['gd'],
		'ktime'=>$ktime,
		'dtime'=>$dtime,
		'msg'=>$msg,
		'dateline'=>$_SGLOBAL['timestamp'],
		'ip'=>getonlineip(),
		'lz'=>$_SCONFIG['sitename'],
		'ischeck'=>1,
		
	);
	
	$cid=inserttable('video_comment',$arr,1);
	
	$message="分享视频<a href='/video.php?id=".$videoid."' target='_blank'><".$video['title']."></a>";
	
	$feedarr = array(
			'appid' => UC_APPID,
			'icon' => 'video',
			'uid' => $_SGLOBAL['supe_uid'],
			'username' => $_SGLOBAL['supe_username'],
			'dateline' => $_SGLOBAL['timestamp'],
			'title_template' => cplang('feed_doing_title'),
			'title_data' => saddslashes(serialize(sstripslashes(array('message'=>$message)))),
			'body_template' => '{msg}',
			'body_data' => saddslashes(serialize(sstripslashes(array('msg'=>$msg)))),
			'id' => $cid,
			'idtype' => 'videocid'
		);
		$feedarr['hash_template'] = md5($feedarr['title_template']."\t".$feedarr['body_template']);//喜好hash
		$feedarr['hash_data'] = md5($feedarr['title_template']."\t".$feedarr['title_data']."\t".$feedarr['body_template']."\t".$feedarr['body_data']);//合并hash
		inserttable('feed', $feedarr);
	getreward('sharevideo');
	rtjson('分享成功!',0);

	exit;
}

if(submitcheck('videosharesubmit')) {
	
	if(empty($_SGLOBAL['supe_uid'])) {
		rtjson('请先登录!');
	}
	$uid=$_SGLOBAL['supe_uid'];
	
	$cid = empty($_POST['cid'])?'0':intval($_POST['cid']);
	$vid = empty($_POST['vid'])?'0':intval($_POST['vid']);
	$ty = $_POST['ty'];
	
	if($vid=='0'){rtjson('参数id不存在!');}
	
	if(empty($ty)){rtjson('参数ty不存在!');}
	
	$msgarr=array(1=>'挖一下',2=>'埋掉它',3=>'转发','收藏','喜欢','骗人');
	
	$tysql="ty='$ty'";
	$table='video_comment';
	
	$tarr=array(1=>'dig',2=>'bury',3=>'zf','sc','xh','pr');
	
	$setsql=$tarr[$ty]."=".$tarr[$ty]."+1";
	$where="cid=".$cid;
	
	if(in_array($ty,array(1,2))){
		
		//$tysql="ty in (1,2)";
		$table='video';
		
		$setsql=$tarr[$ty]."=".$tarr[$ty]."+1";
		$where="videoid=".$vid;
	}

	if(in_array($ty,array(5,6))){
		
		//$tysql="ty in (5,6)";
		
	}
	
	$query=$_SGLOBAL['db']->query("select * from ".tname('video_share')." where uid='$uid' and cid='$cid' and vid='$vid' and $tysql");
	
	if($row=$_SGLOBAL['db']->fetch_array($query)){
		rtjson('已点过'.$msgarr[$ty].'了!');
	}
	
	$_SGLOBAL['db']->query("UPDATE ".tname($table)." SET ".$setsql." WHERE ".$where);
	
	$insarr=array(
		'ty'=>$ty,
		'cid'=>$cid,
		'vid'=>$vid,
		'uid'=>$uid,
		'dateline'=>$_SGLOBAL['timestamp'],
		'ip'=>getonlineip(),
	);
	
	inserttable('video_share',$insarr,0);
	
	if(in_array($ty,array(3,4))){//add feed
	
		$query=$_SGLOBAL['db']->query("select * from ".tname('video')." where videoid=".$vid);

		$video=$_SGLOBAL['db']->fetch_array($query);
		
		$message=$msgarr[$ty]."视频<a href='/video.php?id=".$vid."' target='_blank'><".$video['title']."></a>";
		
		$feedarr = array(
				'appid' => UC_APPID,
				'icon' => 'video',
				'uid' => $_SGLOBAL['supe_uid'],
				'username' => $_SGLOBAL['supe_username'],
				'dateline' => $_SGLOBAL['timestamp'],
				'title_template' => cplang('feed_doing_title'),
				'title_data' => saddslashes(serialize(sstripslashes(array('message'=>$message)))),
				'body_template' => '{msg}',
				'body_data' => saddslashes(serialize(sstripslashes(array('msg'=>$msg)))),
				'id' => $cid,
				'idtype' => 'videocid'
			);
			$feedarr['hash_template'] = md5($feedarr['title_template']."\t".$feedarr['body_template']);//喜好hash
			$feedarr['hash_data'] = md5($feedarr['title_template']."\t".$feedarr['title_data']."\t".$feedarr['body_template']."\t".$feedarr['body_data']);//合并hash
			inserttable('feed', $feedarr);
	}
	
	//getreward('videoshare');
	
	rtjson('1',0);
	exit;
}

$id = empty($_GET['id'])?'1':$_GET['id'];

$query=$_SGLOBAL['db']->query("select * from ".tname('video')." where videoid=".$id);

$video=$_SGLOBAL['db']->fetch_array($query);

$_SGLOBAL['db']->query("update ".tname('video')." set hit=hit+1 where videoid=".$id);

//$video['dateline'] = sgmdate('Y-m-d H:i', $video['dateline']);

$_TPL['titles']=array($video['title']);

//$list=array(1,2,3,4,5,6);

//看点
$query=$_SGLOBAL['db']->query("select * from ".tname('video_comment')." where videoid='$id' and ty='看点' and ischeck=1 order by dateline desc limit 0,6");
$kdlist=array();
while($row=$_SGLOBAL['db']->fetch_array($query)){
$kdlist[]=$row;
}


//悬念
$query=$_SGLOBAL['db']->query("select * from ".tname('video_comment')." where videoid='$id' and ty='悬念' and ischeck=1 order by dateline desc limit 0,6");
$xnlist=array();
while($row=$_SGLOBAL['db']->fetch_array($query)){
$xnlist[]=$row;
}


//分享排行
$query=$_SGLOBAL['db']->query("select count(cid) as num,uid,username from ".tname('video_comment')." group by uid order by num desc limit 0,5");

$fxlist=array();
while($row=$_SGLOBAL['db']->fetch_array($query)){
	$fxlist[]=$row;
}


//他们正在分享
$query=$_SGLOBAL['db']->query("select uid,username from ".tname('video_comment')." group by uid order by dateline desc limit 0,12");

$fxmember=array();
while($row=$_SGLOBAL['db']->fetch_array($query)){
	$fxmember[]=$row;
}


//花絮
$hxlist=array();
$wsql="vid='$id' and ty=1 order by dateline desc limit 0,6";

if($video['ty']==1){
	$wsql="vid='".$video['vid']."' and ty=1 order by dateline desc limit 0,5";
	
$query=$_SGLOBAL['db']->query("select * from ".tname('video')." where videoid=".$video['vid']);

$hxlist[]=$_SGLOBAL['db']->fetch_array($query);

}


$query=$_SGLOBAL['db']->query("select * from ".tname('video')." where $wsql");


while($row=$_SGLOBAL['db']->fetch_array($query)){
	$hxlist[]=$row;
}

?>